home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 1995, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* clipplanes.c
- * This program demonstrates how to define and use arbitrary
- * clipping planes.
- *
- * ESCAPE key - exits program
- * SPACE key - toggles fixed/moving clipping planes
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdio.h>
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid animate( GLvoid );
- GLvoid visibility( GLint );
- GLvoid keyboard( GLubyte, GLint, GLint );
-
- GLvoid drawTorusRing( GLboolean );
-
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static GLboolean movingClipPlanes = GL_TRUE;
-
- static GLfloat angle = 0.0;
-
- GLvoid
- main( int argc, char *argv[] )
- {
- GLsizei width, height, winSize;
-
- glutInit( &argc, argv );
-
- width = glutGet(GLUT_SCREEN_WIDTH);
- height = glutGet(GLUT_SCREEN_HEIGHT);
- winSize = ( width < height ) ? width : height;
- glutInitWindowPosition( winSize/4, winSize/4 );
- glutInitWindowSize( winSize/2, winSize/2 );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutIdleFunc( animate );
- glutVisibilityFunc( visibility );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
- glutMainLoop();
- }
-
- GLvoid
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - demonstrates how to define and use \n"
- "arbitrary clipping planes.\n\n"
- "SPACE key - toggles fixed/moving clipping planes \n"
- "Escape key - exit the program \n\n",
- progname );
- }
-
-
- void
- initgfx( void )
- {
- GLfloat mat_specular[] = { 0.8, 0.8, 0.8, 1.0 };
- GLfloat mat_shininess[] = { 10.0 };
-
- GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
-
- glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
- glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
-
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
-
- glEnable( GL_LIGHTING );
- glEnable( GL_LIGHT0 );
-
- glColorMaterial( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );
- glEnable( GL_COLOR_MATERIAL );
-
- glClearColor( 0, 0, 0, 1 );
- glEnable( GL_DEPTH_TEST );
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case ' ': /* toggle moving clipping plane on and off */
- movingClipPlanes = !movingClipPlanes;
- glutPostRedisplay();
- break;
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- aspect = (GLdouble) width / (GLdouble) height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 45.0, aspect, 3.0, 13.0 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glTranslatef( 0.0, 0.0, -8.0 );
- }
-
- GLvoid
- animate( GLvoid )
- {
- /* update the current angle */
- angle = fmodf( (angle - 1.0), 360.0 );
-
- /* Tell GLUT to redraw the scene */
- glutPostRedisplay();
- }
-
- GLvoid
- visibility( int state )
- {
- if (state == GLUT_VISIBLE) {
- glutIdleFunc( animate );
- } else {
- glutIdleFunc( NULL );
- }
- }
-
- GLvoid
- drawTorusRing( GLboolean solid )
- {
- int i, slices = 8;
- for ( i = 0; i < slices; i++ )
- {
- glColor3f( i/10.0, i/10.0, 1.0 - i/10.0 );
- glPushMatrix();
- glRotatef( i * 360.0/slices, 0, 0, 1 );
- glTranslatef( 1.5, 0.0, 0.0 );
- glRotatef( i * 360.0/slices, 0, 1, 0 );
- if (solid)
- glutSolidTorus( 0.25, 0.75, 8, 15 );
- else
- glutWireTorus( 0.25, 0.75, 8, 15 );
- glPopMatrix();
- }
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- GLdouble planeABCD[4] = { -1.0, 0.0, 0.0, 0.5 };
-
- /* to reverse clipping direction, reverse signs of A, B, C, & D */
- GLdouble oppositePlaneABCD[4] = { 1.0, 0.0, 0.0, -0.5 };
-
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
-
- if ( !movingClipPlanes ) {
- /* define clipping planes fixed in scene */
- glClipPlane (GL_CLIP_PLANE0, planeABCD);
- glClipPlane (GL_CLIP_PLANE1, oppositePlaneABCD);
- }
-
- glPushMatrix();
- glRotatef( angle, 0.0, 1.0, 0.0 );
-
- if ( movingClipPlanes ) {
- /* defining clipping planes after rotation
- * so they stay fixed relative to the objects */
- glClipPlane (GL_CLIP_PLANE0, planeABCD);
- glClipPlane (GL_CLIP_PLANE1, oppositePlaneABCD);
- }
-
- /* clip parts of the solid torii */
- glEnable (GL_CLIP_PLANE0);
- drawTorusRing( GL_TRUE );
- glDisable (GL_CLIP_PLANE0);
-
- /* clip the opposite parts of the wire torii */
- glEnable (GL_CLIP_PLANE1);
- drawTorusRing( GL_FALSE );
- glDisable (GL_CLIP_PLANE1);
- glPopMatrix();
-
- glutSwapBuffers();
- }
-